#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Collections.Generic;
using System.Linq;

#endregion

namespace WaveTech.GShield.Data
{
	public static class ServicesDAO
	{
		#region Get Random Service Url

		public static string GetRandomServiceUrl()
		{
			Random r = new Random(DateTime.UtcNow.Second);
			var serviceUrl = from s in Database.DC.Services
			                 orderby r.Next()
			                 select s;

			return serviceUrl.Single().URL;
		}

		#endregion Get Random Service Url

		public static List<string> GetAllServiceUrls()
		{
			var serviceUrls = from s in Database.DC.Services
			                  select s.URL;

			return serviceUrls.ToList();
		}

		public static void Insert(string url)
		{
			Service s = new Service();
			s.URL = url;
			s.DateAdded = DateTime.Now;

			Database.DC.Services.InsertOnSubmit(s);
			Database.DC.SubmitChanges();
		}

		public static void Delete(string url)
		{
			var service = from s in Database.DC.Services
			              where s.URL == url
			              select s;

			Database.DC.Services.DeleteAllOnSubmit(service);
			Database.DC.SubmitChanges();
		}
	}

	#region Old ServicesDAO

	/*
  /// <summary>
  /// Advisor Web Service Url Data Access Objects
  /// </summary>
  public class ServicesDAO : DAOBase
  {
    #region Get Service Urls
    /// <summary>
    /// Gets all the Advisor Web Service Url's from the database.
    /// </summary>
    /// <returns>Arraylist of all Urls</returns>
    public ArrayList GetServiceUrls()
    {
      ArrayList l = new ArrayList();
      Database db;
      DbCommand dbCommand;

      db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"SELECT [URL]
																						FROM dbo.Services");

      DataSet ds = db.ExecuteDataSet(dbCommand);

      foreach (DataRow r in ds.Tables[0].Rows)
      {
        l.Add(Encryption.Decrypt(r[0].ToString()));
      }

      return l;
    }
    #endregion Get Service Urls

    #region Get Random Service Url
    /// <summary>
    /// Get a single random Advisor Web Service Url from the database.
    /// </summary>
    /// <returns>Advisor Web Service Url</returns>
    public string GetRandomServiceUrl()
    {
      Database db;
      DbCommand dbCommand;

      db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"SELECT TOP 1 [URL]
																						FROM dbo.Services
																						ORDER BY NEWID()");

      return Encryption.Decrypt(db.ExecuteScalar(dbCommand).ToString());
    }
    #endregion Get Random Service Url

    #region Delete
    /// <summary>
    /// Removes an Advisor Web Service Url from the database.
    /// </summary>
    /// <param name="url">Url string to remove</param>
    public void Delete(string url)
    {
      if (url == null)
        throw new ArgumentNullException("url");

      Database db;
      DbCommand dbCommand;

      db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"DELETE
																						FROM Services
																						WHERE ([URL] = @URL)");

      db.AddInParameter(dbCommand, "@URL", DbType.String, Encryption.Encrypt(url));

      db.ExecuteNonQuery(dbCommand);
    }
    #endregion Delete

    #region Insert
    /// <summary>
    /// Inserts a new Advisor Web Service Url into the database
    /// </summary>
    /// <param name="url">Url string to insert</param>
    /// <returns>New Id number of the inserted Url</returns>
    public int? Insert(string url)
    {
      if (url == null || url.Length <= 0)
        throw new ArgumentNullException("url");

      Database db;
      DbCommand dbCommand;
      int? Id;

      db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"INSERT INTO [Services]
																									 ([URL])
																						 VALUES
																									 (@URL)
																					SELECT Scope_Identity()");

      db.AddInParameter(dbCommand, "@URL", DbType.String, Encryption.Encrypt(url));

      Id = int.Parse(db.ExecuteScalar(dbCommand).ToString());

      return Id;
    }
    #endregion Insert
  }
    */

	#endregion Old ServicesDAO
}